<?php
class welcome_model extends MY_Model 
{
    protected $tab = 'withao';
    protected $tab_one = 'product';
    //protected $tab_tow = 'withOther';
    protected $tab_three = 'posts';

    public  function __construct() {
		parent::__construct();
        $this->load->database();
    }
    
   
    /*
    *产品列表
    */
    
    public function get_pro()
    {
        $result = $this->db->get($this->tab_one)->result_array();
        return $result;
    }
    
    /*
    *得到产品详细信息
    */
    public function get_pro_content($id)
    {
        
        $this->db->where('id', $id);
        $re = $this->db->get($this->tab_one)->row_array();
        return $re;
    }
    /*
    *得到产品详细信息
    */
     public function get_pro_contentone()
    {
        $re = $this->db->get($this->tab_one)->row_array();
        return $re;
    }
    /*
    *得到产品详细信息
    */
   public function ajaxget_pro_contentone($id, $type)
   {
       if($type == 'you'){
            $sql = "select * from ao_product where id > {$id} order by id asc  limit 0,1";
       }else{
            $sql = "select * from ao_product where id < {$id} order by id desc  limit 0,1";
       }
        
        $re = $this->db->query($sql);
        $resu = $re->row_array();
        return $resu;
   }
    
    /*
    *得到其它产品详细信息
    */
    
    public function get_otherpro_content($id)
    {
        $re=$this->db->select('id, proname, prodesc, proimg')->from($this->tab_one)->where('id !=', $id)->get()->result_array();
        
        return $re;
    }
    
    /*
    *得到关于他们列表信息,在插入关于他们的文章信息时先插入文章在放图片
    */
    public function get_with_list($type, $ppage)
    {
        
        
        if($type == "more"){
            $where = "";
            $total = $this->countnum($where);
            $pagearr=$this->page($classId, $ppage, $total);//ppag用于ajax请尔
            $num = $pagearr['num'];
            $num = 9;
            $start = $pagearr['start'];
            $sql="select p.*,u.user_login from ao_posts as p,ao_users as u,ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and p.post_author = u.ID and t.term_id = 19 limit {$start},{$num}";
            $res = $this->db->query($sql);
            $resu = $res->result_array();
            foreach($resu as &$va){
                $sql = "select post_excerpt, post_title from ao_posts where post_type = 'attachment' and post_parent = '{$va['ID']}'";
                $r = $this->db->query($sql);
                $re = $r->row_array();
                $va['img'] = $re;
                
               preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$va['post_content'],$match); 
                $va['imgurl']=$match[1];
            }
            $resu['page'] = $pagearr['pa'];
            return $resu;

            
        }else{
            $sql="select p.*,u.user_login from ao_posts as p,ao_users as u,ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and p.post_author = u.ID and t.term_id = 19 limit 0, 5";
            $res = $this->db->query($sql);
            $resu = $res->result_array();
            foreach($resu as &$va){
                $sql = "select post_excerpt, post_title from ao_posts where post_type = 'attachment' and post_parent = '{$va['ID']}'";
                $r = $this->db->query($sql);
                $re = $r->row_array();
                $va['img'] = $re;
                preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$va['post_content'],$match); 
                $va['imgurl']=$match[1];
            }
            return $resu;
            
               }
        
    }
    
    private function countnum($where){
        $sql="select p.*,u.user_login from ao_posts as p,ao_users as u,ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and p.post_author = u.ID and t.term_id = 19";
            $res = $this->db->query($sql);
            $resu = $res->result_array();
            return count($resu);
    }
    
    /*
    此为取的单个关于他们的信息
    */
    public function get_rowOther($id)
    {
        $sql="select p.*,u.user_login from ao_posts as p,ao_users as u,ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and p.post_author = u.ID and t.term_id = 19 and p.ID = {$id}";
        $res = $this->db->query($sql);
        $resu = $res->row_array();
        $sql = "select post_excerpt, post_title from ao_posts where post_type = 'attachment' and post_parent = '{$resu['ID']}'";
        $re= $this->db->query($sql);
        $result = $re->row_array();
        preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i', $resu['post_content'],$match); 
        $resu['img']=$match[1];
        $resu['imginfo']=$result;
        return $resu;
    }
    
    /*
    *搜索文章
    */
    
    public function get_search_article($searchID, $ppage)
    {
        if($searchID == "no"){
            $where="";
            $total = $this->total($this->tab_three, $where);
            $page = $this->page($where, $ppage, $total);
            $num = $page['num'];
            $start = $page['start'];
          
            $this->db->where('post_status', 'publish');
            $this->db->limit(10 ,$start);
            $result = $this->db->get($this->tab_three)->result_array();
            $result['page'] = $page['pa'];
            return $result;
        }else{
            $one = $searchID;
            $tow = $searchID;
            $three = $searchID;
            $where="";
            $total = $this->total($this->tab_three, $where);
            $page = $this->page($where, $ppage, $total);
            $num = $page['num'];
            $start = $page['start'];
            $this->db->where('post_status', 'publish');
            $this->db->limit(20 ,$start);
            $this->db->like('post_title', $one);
            //$this->db->or_like('post_date', $tow);
            //$this->db->or_like('post_content',$three);
            $result = $this->db->get($this->tab_three)->result_array();
            $result['page'] = $page['pa'];
            return $result;
        }
    }
    
    private function total($table,$where)
   {
        
        $this->db->where('post_status', 'publish');
        $re = $this->db->get($table)->result_array();
        $total = count($re);
        return $total;
        
   }
   
   /*
    *首页的专业名词名称展示
    */
   public function get_nounname_list()
   {
       $sql="select p.*, u.user_login from ao_posts as p,ao_users as u,ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and p.post_author = u.ID and t.term_id = 20 limit 0, 20";
        $re = $this->db->query($sql);
        $resu = $re->result_array();
        return $resu;
   }
    
    /*
    *分组后的专业名词
    */
    public function get_moreNounName_list()
    {
        $noun=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
       $arr = array();
       foreach($noun as $k=>$l){
       $sql = "select p.post_title, p.ID from ao_posts as p, ao_terms as t, ao_term_relationships as tr where p.ID = tr.object_id and tr.term_taxonomy_id=t.term_id and lower(t.name) = lower('{$l}')";
       $re= $this->db->query($sql);
       $resu = $re->result_array();
       if(!$resu){
           continue;
       }
       $arr[strtolower($l)]=$resu;
       }
       return $arr;
    }
    
    
   
    
    
}